package com.example;

import com.example.log.Logs;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.time.LocalDateTime;
import java.util.Objects;

public class App extends Application {

    @Override
    public void init() throws Exception {
        super.init();
        Logs.getInstance().log("应用程序初始化开始...");
    }

    public static void main(String[] args) {
        try {
            Logs.getInstance().log("主程序启动...");
            launch(args);
        } catch (Exception e) {
            Logs.getInstance().log("主程序异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Logs.getInstance().log("应用程序启动时间: " + LocalDateTime.now());
        Logs.getInstance().log("开始加载FXML文件...");

        try {
            // 加载FXML
            Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("view/WebHttpView.fxml")));
            Logs.getInstance().log("FXML文件加载成功");

            // 设置窗口
            Logs.getInstance().log("开始配置主窗口...");
            primaryStage.setTitle("HTTP Client");
            primaryStage.setScene(new Scene(root, 1000, 800));
            Logs.getInstance().log("主窗口配置完成");

            // 添加窗口关闭事件处理
            primaryStage.setOnCloseRequest(event -> {
                Logs.getInstance().log("应用程序开始关闭...");
                System.exit(0);
            });

            Logs.getInstance().log("显示主窗口");
            primaryStage.show();
            Logs.getInstance().log("应用程序启动完成");

        } catch (Exception e) {
            Logs.getInstance().log("启动过程发生异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        Logs.getInstance().log("应用程序正常停止");
    }
}
